*Carola Binder and Christos Makridis
*"Stuck in the Seventies: Gas Prices and Consumer Sentiment"

set scheme s1mono


/***************** legend

Figures:
Figure 1: Real Gas Prices and Consumer Sentiment
Figure 2: Dynamic Response of Consumer Sentiment to Gas Prices
Figure 3: Heterogeneity of Gas Prices and Consumption Across States

Main Tables:
Table 1: Effects of Gas Prices on Economic Sentiment 
Table 2: Effects of Gas Prices on Economic Sentiment at Alternative Frequencies
Table 3: Tests for Asymmetric Effects of Gas Prices on Economic Sentiment
Table 4: Demographic Heterogeneity in Effects of Gas Prices on Sentiment
Table 5: Response of Sentiment to Gas Prices by State

Appendix:
Figure A.1 Comparison of Gallup and Michigan Survey of Consumers Data
Figure A.2 Elasticity of Sentiment to Gas Price by Age Cohort
Table A.1 Summary Statistics for U.S. Gallup Daily Data
Table A.2 Regressions with Alternative Dependent Variables
Table A.3 Correlations of Economic Sentiment and Subjective Well-Being Variables

Cleaning:
Social explorer zipcode
BEA Consumption/capita
Estimate state gas price elasticities


Note: us_daily_all_clean_consolidate.dta and OPIS_clean.dta are not included in the data folder because it requires proprietary access to Gallup, but this can be obtained through a standard procurement order.

******************/


*-------------------------------- Figure 1: Real Gas Prices and Consumer Sentiment

use OPIS_clean.dta,clear
merge m:1 st using st2005.dta
keep if _merge==3
drop _merge
collapse price_nsadefl [aw=totpop],by(year month day)
save OPIS_daily_national.dta,replace

use us_daily_all_clean_consolidate.dta,clear
drop day
gen day=day(intdate)
gen totsent=state_of_econ+future_of_econ
collapse (mean) totsent_mu=totsent state_mu=state_of_econ future_mu=future_of_econ (sd) totsent_sd=totsent future_sd=future_of_econ lifeladder_sd=lifeladder_now now_sd=state_of_econ ///
	[aw=COMB_WEIGHT],by(year month day intdate)
ren intdate date
merge 1:1 year month day using OPIS_daily_national.dta
keep if _merge==3
drop _merge
	
tostring date,gen(strdate)
destring strdate,replace

corr totsent_mu price_nsadefl
local temp: di %3.2f r(rho)
twoway (line totsent_mu date, clcolor(gray) clwidth(medthin) yaxis(1) ytitle("Economic Sentiment", axis(1)) clpattern(solid) ) (line price_nsadefl date, clcolor(black) clwidth(medthin) yaxis(2) ytitle("Gas Prices", axis(2)) clpattern(dash) ), xtitle("") legend( order( 1 "Economic Sentiment" 2 "Gas Prices") ) xscale(range(17533 21092)) xlabel(, format(%tdCCYY)) note(correlation = `temp')
graph export Figure1.pdf, as(pdf) replace

*-------------------------------- Figure 2: Dynamic Response of Consumer Sentiment to Gas Prices

use OPIS_clean.dta,clear
egen tid=group(year month day)
tsset st tid
capture drop lag*
quietly forvalues j=0/40 {
    gen lag`j'_price_nsadefl=L`j'.price_nsadefl
}
save OPIS_clean_lags_forwards.dta,replace

use us_daily_all_clean.dta,clear
drop  healthprob_days rel_* hinsure_covered- hinsure_cov_medicaid
drop if age==.
drop if lifeladder_now==. | lifeladder_future==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen totsent=state_of_econ+future_of_econ
gen day=day(intdate)
merge m:1 st year month day using OPIS_clean_lags_forwards.dta
keep if _merge==3
drop _merge
gen week=week(intdate)
gen age2=age^2

center totsent state_of_econ future_of_econ,prefix(z_) standardize

global gallupX employed married male age age2 educ_nohs educ_tech educ_somecoll educ_coll educ_postgrad race_white race_black
global lags lag1_price_nsadefl lag2_price_nsadefl lag3_price_nsadefl lag4_price_nsadefl lag5_price_nsadefl lag6_price_nsadefl lag7_price_nsadefl lag8_price_nsadefl lag9_price_nsadefl lag10_price_nsadefl lag11_price_nsadefl lag12_price_nsadefl lag13_price_nsadefl lag14_price_nsadefl lag15_price_nsadefl lag16_price_nsadefl lag17_price_nsadefl lag18_price_nsadefl lag19_price_nsadefl lag20_price_nsadefl

gen coef=.
gen se=.
gen id=_n if _n<=41

reghdfe z_totsent price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
replace coef=_b[price_nsadefl] if id==1
replace se=_se[price_nsadefl] if id==1

quietly forvalues j=0/20 {
   reghdfe z_totsent lag`j'_price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)  
    replace coef=_b[lag`j'_price_nsadefl] if id==`j'+1
	replace se=_se[lag`j'_price_nsadefl] if id==`j'+1
}

list id coef se if id<21

drop if coef==.
gen ub=coef+1.96*se
gen lb=coef-1.96*se

gen t=id-1
keep if t<9
tsset t
twoway tsline coef ub lb, lpattern(solid dash dash) lcolor(black gray gray) legend(order(1 2) label(1 "Point estimate") label(2 "95% confidence interval") cols(1) ring(0) bplacement(se)) ytitle("Elasticity of Economic Sentiment to Gas Prices") xtitle("Time Period (t) = Days")
graph export Figure2.pdf, as(pdf) replace

*-------------------------------- Figure 3: Heterogeneity of Gas Prices and Consumption Across States
*And
*Table 5: Response of Sentiment to Gas Prices by State

insheet using OPIS Retail State Averages_2008to2018.csv, clear
drop if date=="" | date=="Copyright 2018"
gen year=substr(date,-4,4)
gen month=substr(date,1,1) if substr(date,2,1)=="/"
replace month=substr(date,1,2) if substr(date,2,1)!="/"
gen day=substr(date,-6,1) if substr(date,-7,1)=="/"
replace day=substr(date,-7,2) if substr(date,-8,1)=="/"
destring year month day,replace
ren summaryregionname state
gen st=.
replace st=99 if state=="AB"
replace st=2 if state=="AK"
replace st=1 if state=="AL"
replace st=5 if state=="AR"
replace st=4 if state=="AZ"
replace st=99 if state=="BC"
replace st=6 if state=="CA"
replace st=8 if state=="CO"
replace st=9 if state=="CT"
replace st=11 if state=="DC"
replace st=10 if state=="DE"
replace st=12 if state=="FL"
replace st=13 if state=="GA"
replace st=99 if state=="GU"
replace st=15 if state=="HI"
replace st=19 if state=="IA"
replace st=16 if state=="ID"
replace st=17 if state=="IL"
replace st=18 if state=="IN"
replace st=20 if state=="KS"
replace st=21 if state=="KY"
replace st=22 if state=="LA"
replace st=25 if state=="MA"
replace st=99 if state=="MB"
replace st=24 if state=="MD"
replace st=23 if state=="ME"
replace st=26 if state=="MI"
replace st=27 if state=="MN"
replace st=29 if state=="MO"
replace st=28 if state=="MS"
replace st=30 if state=="MT"
replace st=99 if state=="NB"
replace st=37 if state=="NC"
replace st=38 if state=="ND"
replace st=31 if state=="NE"
replace st=99 if state=="NF"
replace st=33 if state=="NH"
replace st=34 if state=="NJ"
replace st=35 if state=="NM"
replace st=99 if state=="NS"
replace st=32 if state=="NV"
replace st=36 if state=="NY"
replace st=39 if state=="OH"
replace st=40 if state=="OK"
replace st=99 if state=="ON"
replace st=41 if state=="OR"
replace st=42 if state=="PA"
replace st=99 if state=="PE"
replace st=99 if state=="PR"
replace st=99 if state=="QC"
replace st=44 if state=="RI"
replace st=45 if state=="SC"
replace st=46 if state=="SD"
replace st=99 if state=="SK"
replace st=47 if state=="TN"
replace st=48 if state=="TX"
replace st=49 if state=="UT"
replace st=51 if state=="VA"
replace st=99 if state=="VI"
replace st=50 if state=="VT"
replace st=53 if state=="WA"
replace st=55 if state=="WI"
replace st=54 if state=="WV"
replace st=56 if state=="WY"
drop if st==. // these are just the 5 regions that are aggregated of some states
keep year month day st state pricecount stationcount price
merge m:1 year month using stlouis_monthly_pce.dta
keep if _merge==3
drop _merge
gen price_nsadefl=price*100/pce2012
la var price_nsadefl "gasoline price, PCE deflated 2012"
egen tid=group(year month day)
tsset st tid
gen lag1_price_nsadefl=L1.price_nsadefl
gen lag2_price_nsadefl=L2.price_nsadefl
gen lag7_price_nsadefl=L7.price_nsadefl
gen lag8_price_nsadefl=L8.price_nsadefl

gen dl1price_nsadefl=(price_nsadefl-L.price_nsadefl)/L.price_nsadefl
gen dprice=price-l.price
gen absdprice=abs(dprice)
drop tid pce2012 pricecount stationcount


collapse st price_nsadefl price absdprice (sd) pricevol=price_nsadefl returnvol=dl1price_nsadefl nompricevol=price (iqr) priceiqr=price_nsadefl nompriceiqr=price (max) maxabsdprice=absdprice nompricemax=price pricemax=price_ns (min) nompricemin=price pricemin=price_ns, by(state)

merge 1:1 state using gaspercapita.dta
drop _merge
merge 1:1 state using realgdp.dta
drop _merge
merge 1:1 state using population.dta
drop _merge
merge 1:1 st using state_gasprice_elasticities.dta
drop _merge
gen abscoef=abs(coef)
gen realgdppercap=realgdp/population

replace gasper=gasper/1000
drop if st==.
label var price_ "Mean real gas price"
label var pricevol "Daily real gas price volatility"
label var gaspercapita "Annual gas per capita (\$1000s)"
gen gallonspercapita=gaspercapita*1000/price
label var gallonspercapita "Gallons per capita"
label var price "Gas price"
label var nompricevol "Gas price volatility"

eststo clear
eststo: quietly reg coef gasper [aw=population], r
eststo: quietly reg coef gallonsper [aw=population], r
eststo: quietly reg coef gasper price realgdpper [aw=population], r
eststo: quietly reg coef gasper price realgdpper [aw=population] if state!="DC", r
eststo: quietly reg coef gasper nompricevol [aw=population] if state!="DC", r
esttab, label se star(* 0.10 ** 0.05 *** 0.01) replace stats (r2 N, label("R-squared" "Sample Size") fmt(2 0)) parentheses nolz nogaps fragment nolines mtitles("(1)" "(2)" "(3)" "(4)" "(5)")


maptile price_,geo(state) nquant(4) twopt(title("A. Gas Price", size(vlarge))) rangecolor(gs12 black)
graph export Figure3a.pdf, as(pdf) replace
maptile gaspercap,geo(state) nquant(4) twopt(title("B. Annual Gas Consumption per Capita (Thousands of $)", size(vlarge))) rangecolor(gs12 black)
graph export Figure3b.pdf, as(pdf) replace
maptile coef,geo(state) nquant(4) twopt(title("C. Response of Sentiment to Gas Price", size(vlarge))) revcolor rangecolor(gs12 black)
graph export Figure3c.pdf, as(pdf) replace


*-------------------------------- Table 1: Effects of Gas Prices on Economic Sentiment 

use us_daily_all_clean.dta, clear
drop  healthprob_days rel_* hinsure_covered- hinsure_cov_medicaid
drop if age==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen totsent=state_of_econ+future_of_econ
gen day=day(intdate)
merge m:1 st year month day using OPIS_clean.dta
keep if _merge==3
drop _merge
merge m:1 zip using zip10_14.dta
keep if _merge==3 | _merge==1
drop _merge
gen age2=age^2
egen tid=group(intdate)
gen lprice_nsadefl=log(price_nsadefl+1)
gen coh=year-age
gen agrp=0
replace agrp=1 if coh<1962 // born in 1962 or earlier --> 18+ in 1980 oil crisis
replace agrp=. if age==.
gen college=cond(educ_coll==1 | educ_postgrad==1,1,0)
gen highimpactst=. // jp morgan measure of "higher-impact states" (just do control F)
replace highimpactst=1 if st==19 | st==18 | st==20 | st==26 | st==29 | st==39 | st==46
replace highimpactst=1 if st==1 | st==21 | st==22 | st==28 | st==40 | st==47 | st==48
replace highimpactst=0 if st==9 | st==10 | st==11 | st==12 | st==25 | st==24 | st==37 | st==36 | st==42
replace highimpactst=0 if st==2 | st==6 | st==15 | st==32 | st==41 | st==53
quietly sum income,d
gen highpersinc=cond(income>=`r(p50)',1,0)
replace highpersinc=. if income==.
gen price_nsadefl_agrp=price_nsadefl*agrp
gen price_nsadefl_coll=price_nsadefl*college
gen price_nsadefl_inc=price_nsadefl*highincome
gen price_nsadefl_persinc=price_nsadefl*highpersinc
gen price_nsadefl_comm=price_nsadefl*highcommute
gen price_nsadefl_highimpactst=price_nsadefl*highimpactst


center totsent state_of_econ future_of_econ lifeladder_now lifeladder_future,prefix(z_) standardize

global gallupX employed married male age age2 educ_nohs educ_tech educ_somecoll educ_coll educ_postgrad race_white race_black
global gallupXage employed married male educ_nohs educ_tech educ_somecoll educ_coll educ_postgrad race_white race_black
global gallupXcoll employed married male age age2 race_white race_black
global gallupXcoll2 employed married male age age2 educ_nohs educ_tech educ_somecoll race_white race_black
global gallupXeduc employed married male age age2 college race_white race_black

*compare with coefficient on college
reghdfe z_totsent price_nsadefl $gallupXeduc [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)

*quietly oprobit totsent price_nsadefl $gallupX [aw=COMB_WEIGHT],cluster(st)
*margins, dydx(price_nsadefl)

*main results
reg z_totsent price_nsadefl $gallupX [aw=COMB_WEIGHT],cluster(st)
estadd local hasX "Yes",replace
estadd local hasstt "No",replace
estadd local hast "No",replace
estadd local hasIV "No",replace
estadd local sampl "Full",replace
est sto temp1
reghdfe z_totsent price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "Full",replace
est sto temp2
reghdfe z_state_of_econ price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "Full",replace
est sto temp3
reghdfe z_future_of_econ price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "Full",replace
est sto temp4
reghdfe goodecon price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "Full",replace
est sto temp5
reghdfe getbetter price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "Full",replace
est sto temp6

la var price_nsadefl "State Gas Price"
esttab temp1 temp2 temp3 temp4 temp5 temp6 , b(3) replace star(* 0.10 ** 0.05 *** 0.01) mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") nonum brackets se mgroups("Economic Sentiment (z-score)" "Current Sentiment (z-score)" "Future Sentiment (z-score)" "Economy is Good (1/0)" "Economy is Improving (1/0)", pattern(1 0 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) label keep(price_nsadefl) stats (r2 N hasX hasstt hast, label("R-squared" "Sample Size" "Controls" "State x Year x Quarter FE" "Day of the Year FE") fmt(2 0)) parentheses nolz nogaps fragment nolines prehead("Dep. var. = ")

 

*-------------------------------- Table 2: Effects of Gas Prices on Economic Sentiment at Alternative Frequencies

use us_daily_all_clean.dta,clear
drop  healthprob_days rel_* hinsure_covered- hinsure_cov_medicaid
drop if age==.
drop if lifeladder_now==. | lifeladder_future==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen totsent=state_of_econ+future_of_econ
gen day=day(intdate)
merge m:1 st year month day using OPIS_clean.dta
keep if _merge==3
drop _merge
gen age2=age^2
egen tid=group(intdate)
gen ltotsent=log(totsent)

center totsent state_of_econ future_of_econ,prefix(z_) standardize

global gallupX employed married male age age2 educ_nohs educ_tech educ_somecoll educ_coll educ_postgrad race_white race_black

reghdfe z_totsent price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hasday "Yes",replace
estadd local hasweek "No",replace
estadd local hasmonth "No",replace
est sto temp1
reghdfe z_totsent gaspriceweek $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hasday "Yes",replace
estadd local hasweek "No",replace
estadd local hasmonth "No",replace
est sto temp2
reghdfe z_totsent gaspricemonth $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hasday "Yes",replace
estadd local hasweek "No",replace
estadd local hasmonth "No",replace
est sto temp3
reghdfe getbetter price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hasday "Yes",replace
estadd local hasweek "No",replace
estadd local hasmonth "No",replace
est sto temp4
reghdfe getbetter gaspriceweek $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hasday "Yes",replace
estadd local hasweek "No",replace
estadd local hasmonth "No",replace
est sto temp5
reghdfe getbetter gaspricemonth $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hasday "Yes",replace
estadd local hasweek "No",replace
estadd local hasmonth "No",replace
est sto temp6

la var price_nsadefl "State Gas Price (Daily)"
la var gaspriceweek "State Gas Price (Weekly)"
la var gaspricemonth "State Gas Price (Monthly)"
esttab temp1 temp2 temp3 temp4 temp5 temp6 , b(3) replace star(* 0.10 ** 0.05 *** 0.01) mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") nonum brackets se mgroups("Economic Sentiment (z-score)" "Economy is Improving (1/0)", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) label keep(price_nsadefl gaspriceweek gaspricemonth) stats (r2 N hasX hasstt hasday, label("R-squared" "Sample Size" "Controls" "State x Year x Quarter FE" "Day of the Year FE") fmt(2 0)) parentheses nolz nogaps fragment nolines prehead("Dep. var. = ")


*-------------------------------- Table 3: Tests for Asymmetric Effects of Gas Prices on Economic Sentiment

use us_daily_all_clean.dta, clear
drop  healthprob_days rel_* hinsure_covered- hinsure_cov_medicaid
drop if age==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen totsent=state_of_econ+future_of_econ
gen day=day(intdate)
merge m:1 st year month day using OPIS_clean.dta
keep if _merge==3
drop _merge
egen tid=group(intdate)
gen pricegrowth=cond(dlprice_nsadef>0,1,0)
gen price_nsadefl_abovemed=price_nsadefl*price_abovemed
gen price_nsadefl_above75pc=price_nsadefl*price_above75pc
gen price_nsadefl_pricegrowth=price_nsadefl*pricegrowth

center totsent,prefix(z_) standardize

global gallupX employed married male age educ_nohs educ_tech educ_somecoll educ_coll educ_postgrad race_white race_black

reghdfe z_totsent price_nsadefl pricegrowth price_nsadefl_pricegrowth $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasst "Yes",replace
estadd local hast "Yes",replace
est sto temp1
reghdfe z_totsent price_nsadefl price_abovemed price_nsadefl_abovemed $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasst "Yes",replace
estadd local hast "Yes",replace
est sto temp2
reghdfe getbetter price_nsadefl pricegrowth price_nsadefl_pricegrowth $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasst "Yes",replace
estadd local hast "Yes",replace
est sto temp3
reghdfe getbetter price_nsadefl price_abovemed price_nsadefl_abovemed $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasst "Yes",replace
estadd local hast "Yes",replace
est sto temp4

la var price_nsadefl "State Gas Price"
la var price_abovemed "Gas Price Above Median"
la var price_above75pc "Gas Price Above 75 Pctle"
la var pricegrowth "Gas Price Growth is Positive"
la var price_nsadefl_abovemed "\quad $\times$ Gas Price Above Median"
la var price_nsadefl_above75pc "\quad $\times$ Gas Price Above Median"
la var price_nsadefl_pricegrowth "\quad $\times$ Gas Price Growth is Positive"
local tokeep "pricegrowth price_abovemed price_nsadefl price_nsadefl_pricegrowth price_nsadefl_abovemed"

esttab temp1 temp2 temp3 temp4 , b(3) replace star(* 0.10 ** 0.05 *** 0.01) mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") nonum brackets se mgroups("Economic Sentiment (z-score)" "1[Economy is Improving]", pattern(1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) label keep(`tokeep') order(`tokeep') stats (r2 N hasX hasst hast, label("R-squared" "Sample Size" "Controls" "State x Year x Quarter FE" "Day of the Year FE") fmt(2 0)) parentheses nolz nogaps fragment nolines prehead("Dep. var. = ")


*-------------------------------- Table 4: Demographic Heterogeneity in Effects of Gas Prices on Sentiment


use us_daily_all_clean.dta, clear
drop  healthprob_days rel_* hinsure_covered- hinsure_cov_medicaid
drop if age==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen totsent=state_of_econ+future_of_econ
gen day=day(intdate)
merge m:1 st year month day using OPIS_clean.dta
keep if _merge==3
drop _merge
merge m:1 zip using zip10_14.dta
keep if _merge==3 | _merge==1
drop _merge
gen age2=age^2
egen tid=group(intdate)
gen lprice_nsadefl=log(price_nsadefl+1)
gen coh=year-age
gen agrp=0
replace agrp=1 if coh<1962 // born in 1962 or earlier --> 18+ in 1980 oil crisis
replace agrp=. if age==.
gen college=cond(educ_coll==1 | educ_postgrad==1,1,0)
gen highimpactst=. // jp morgan measure of "higher-impact states" (just do control F)
replace highimpactst=1 if st==19 | st==18 | st==20 | st==26 | st==29 | st==39 | st==46
replace highimpactst=1 if st==1 | st==21 | st==22 | st==28 | st==40 | st==47 | st==48
replace highimpactst=0 if st==9 | st==10 | st==11 | st==12 | st==25 | st==24 | st==37 | st==36 | st==42
replace highimpactst=0 if st==2 | st==6 | st==15 | st==32 | st==41 | st==53
quietly sum income,d
gen highpersinc=cond(income>=`r(p50)',1,0)
replace highpersinc=. if income==.
gen price_nsadefl_agrp=price_nsadefl*agrp
gen price_nsadefl_coll=price_nsadefl*college
gen price_nsadefl_inc=price_nsadefl*highincome
gen price_nsadefl_persinc=price_nsadefl*highpersinc
gen price_nsadefl_comm=price_nsadefl*highcommute
gen price_nsadefl_highimpactst=price_nsadefl*highimpactst


center totsent state_of_econ future_of_econ lifeladder_now lifeladder_future,prefix(z_) standardize

global gallupX employed married male age age2 educ_nohs educ_tech educ_somecoll educ_coll educ_postgrad race_white race_black
global gallupXage employed married male educ_nohs educ_tech educ_somecoll educ_coll educ_postgrad race_white race_black
global gallupXcoll employed married male age age2 race_white race_black
global gallupXcoll2 employed married male age age2 educ_nohs educ_tech educ_somecoll race_white race_black
global gallupXeduc employed married male age age2 college race_white race_black


reghdfe z_totsent price_nsadefl agrp price_nsadefl_agrp $gallupXage [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "Full",replace
est sto temp11
reghdfe z_totsent price_nsadefl price_nsadefl_coll college $gallupXcoll [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "Full",replace
est sto temp12
reghdfe z_totsent price_nsadefl price_nsadefl_coll college $gallupXcoll if agrp==1 [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "coh<1962",replace
est sto temp13
reghdfe z_totsent price_nsadefl price_nsadefl_coll college $gallupXcoll if agrp==0 [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "coh>1962",replace
est sto temp14
reghdfe z_totsent price_nsadefl price_nsadefl_inc $gallupX [aw=COMB_WEIGHT],a(st#year#qtr zip tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
estadd local sampl "coh<1962",replace
est sto temp15
reghdfe z_totsent price_nsadefl price_nsadefl_comm $gallupX [aw=COMB_WEIGHT],a(st#year#qtr zip tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
estadd local hasIV "No",replace
est sto temp16


la var price_nsadefl "State Gas Price"
la var price_nsadefl_agrp "\quad $\times$ 1[Born $<$ 1962]"
la var price_nsadefl_coll "\quad $\times$ 1[College]"
la var price_nsadefl_comm "\quad $\times$ 1[Zip Commute $>$ 20min]"
la var price_nsadefl_highimpactst "\quad $\times$ 1[High Impact State]"
la var price_nsadefl_inc "\quad $\times$ 1[Zip Income $>$ 48K]"
la var employed "Employed"
la var married "Married"
la var male "Male"
la var age "Age"
la var age2 "Age (Squared)"
la var educ_nohs "No High School"
la var educ_tech "Technical Training"
la var educ_somecoll "Some College"
la var educ_coll "College"
la var educ_postgrad "Postgrad"
la var race_white "White"
la var race_black "Black"

local tokeep "price_nsadefl price_nsadefl_agrp price_nsadefl_coll price_nsadefl_inc price_nsadefl_comm"
esttab temp11 temp12 temp13 temp14 temp15 temp16 , b(3) replace star(* 0.10 ** 0.05 *** 0.01) mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") nonum brackets se mgroups("Economic sentiment", pattern(1 0 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) label keep(`tokeep') order(`tokeep') stats (r2 N hasX hasstt hast sampl, label("R-squared" "Sample Size" "Controls" "State x Year x Quarter FE" "Day of the Year FE" "Sample") fmt(2 0)) parentheses nolz nogaps fragment nolines prehead("Dep. var. = ")


*-------------------------------- Figure A.1 Comparison of Gallup and Michigan Survey of Consumers Data

use us_daily_all_clean_consolidate.dta, clear
drop if future_of_econ==. | state_of_econ==.
gen college=cond(educ_coll==1 | educ_postgrad==1,1,0)
gen badfutureecon=cond(future_of_econ==1,1,0)
gen badcurrentecon=cond(state_of_econ==1,1,0)
gen badfutureecon_coll=cond(future_of_econ==1 & college==1,1,0)
replace badfutureecon_coll=. if college==0
gen badcurrentecon_coll=cond(state_of_econ==1 & college==1,1,0)
replace badcurrentecon_coll=. if college==0
gen badfutureecon_scoll=cond(future_of_econ==1 & educ_somecoll==1,1,0)
replace badfutureecon_scoll=. if educ_somecoll==0
gen badcurrentecon_scoll=cond(state_of_econ==1 & educ_somecoll==1,1,0)
replace badcurrentecon_scoll=. if educ_somecoll==0
gen goodfutureecon=cond(future_of_econ==3,1,0)
gen goodcurrentecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
collapse (mean) cons_nondur future_of_econ lifeladder_now state_of_econ ///
	bad* good* [aw=COMB_WEIGHT],by(year month)
merge 1:1 year month using MichiganSurveyTimeSeries.dta
keep if _merge==3
drop _merge

replace bago_f_all=bago_f_all/100
replace bago_s_all=bago_s_all/100
replace bago_u_all=bago_u_all/100
replace bexp_f_all=bexp_f_all/100
replace bexp_s_all=bexp_s_all/100
replace bexp_u_all=bexp_u_all/100
replace bago_f_ecd=bago_f_ecd/100
replace bago_s_ecd=bago_s_ecd/100
replace bago_u_ecd=bago_u_ecd/100
replace bexp_f_ecd=bexp_f_ecd/100
replace bexp_s_ecd=bexp_s_ecd/100
replace bexp_u_ecd=bexp_u_ecd/100
replace bago_f_esc=bago_f_esc/100
replace bago_s_esc=bago_s_esc/100
replace bago_u_esc=bago_u_esc/100
replace bexp_f_esc=bexp_f_esc/100
replace bexp_s_esc=bexp_s_esc/100
replace bexp_u_esc=bexp_u_esc/100

gen x=ym(year, month) // first convert a new var into year/month format
tostring x,replace
gen stringx=x
destring x,replace
format x %tm // label it for stata to recognize sequencing

corr badcurrentecon bago_u_all
local temp: di %3.2f r(rho)
twoway (line badcurrentecon x, clcolor(black) clwidth(medthin) clpattern(solid) ) (line bago_u_all x, clcolor(gray) clwidth(medthin) clpattern(dash) ) , xtitle("Month / Year") legend( order( 1 "Gallup" 2 "Michigan") ) ytitle("Perception of Bad Current economy (Share)") subtitle("Panel A: Current State of Economy") note(Correlation = `temp') xscale(range(576 689)) xlabel(576(12)689, angle(45))
graph export FigureA1a.pdf, as(pdf) replace

corr badfutureecon bexp_u_all
local temp: di %3.2f r(rho)
twoway (line badfutureecon x, clcolor(black) clwidth(medthin) clpattern(solid) ) (line bexp_u_all x, clcolor(gray) clwidth(medthin) clpattern(dash) ), xtitle("Month / Year") legend( order( 1 "Gallup" 2 "Michigan") ) ytitle("Perception of Bad Future Economy (Share)") subtitle("Panel B: Future State of Economy") note(Correlation = `temp') xscale(range(576 689)) xlabel(576(12)689, angle(45))
graph export FigureA1b.pdf, as(pdf) replace


*-------------------------------- Figure A.2 Elasticity of Sentiment to Gas Price by Age Cohort

use stlouis_cpigas_annual.dta, clear
merge 1:1 year using stlouis_cpiall_annual.dta
keep if _merge==3
drop _merge
gen cpi_gas_by_all=cpi_gas/cpiall
replace year=year-16 // to get formative years of the individual
ren year coh
gen cgrp=1 if coh>=1950 & coh<=1951
replace cgrp=2 if coh>=1952 & coh<=1953
replace cgrp=3 if coh>=1954 & coh<=1955
replace cgrp=4 if coh>=1956 & coh<=1957
replace cgrp=5 if coh>=1958 & coh<=1959
replace cgrp=6 if coh>=1960 & coh<=1961
replace cgrp=7 if coh>=1962 & coh<=1963
replace cgrp=8 if coh>=1964 & coh<=1965
replace cgrp=9 if coh>=1966 & coh<=1967
replace cgrp=10 if coh>=1968 & coh<=1969
replace cgrp=11 if coh>=1970 & coh<=1971
replace cgrp=12 if coh>=1972 & coh<=1973
replace cgrp=13 if coh>=1974 & coh<=1975
replace cgrp=14 if coh>=1976 & coh<=1977
replace cgrp=15 if coh>=1978 & coh<=1979
replace cgrp=16 if coh>=1980 & coh<=1981
replace cgrp=17 if coh>=1982 & coh<=1983
replace cgrp=18 if coh>=1984 & coh<=1985
replace cgrp=19 if coh>=1986 & coh<=1987
replace cgrp=20 if coh>=1988 & coh<=1989
collapse cpi_gas cpiall cpi_gas_by_all,by(cgrp)
save stlouis_cpigas_annual_cohort.dta,replace

use us_daily_all_clean.dta,clear
drop  healthprob_days rel_* hinsure_covered- hinsure_cov_medicaid
drop if age==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen totsent=state_of_econ+future_of_econ
gen day=day(intdate)
merge m:1 st year month day using OPIS_clean.dta
keep if _merge==3
drop _merge
gen age2=age^2
egen tid=group(intdate)
gen lprice_nsadefl=log(price_nsadefl+1)
gen coh=year-age

gen cgrp=1 if coh>=1950 & coh<=1951
replace cgrp=2 if coh>=1952 & coh<=1953
replace cgrp=3 if coh>=1954 & coh<=1955
replace cgrp=4 if coh>=1956 & coh<=1957
replace cgrp=5 if coh>=1958 & coh<=1959
replace cgrp=6 if coh>=1960 & coh<=1961
replace cgrp=7 if coh>=1962 & coh<=1963
replace cgrp=8 if coh>=1964 & coh<=1965
replace cgrp=9 if coh>=1966 & coh<=1967
replace cgrp=10 if coh>=1968 & coh<=1969
replace cgrp=11 if coh>=1970 & coh<=1971
replace cgrp=12 if coh>=1972 & coh<=1973
replace cgrp=13 if coh>=1974 & coh<=1975
replace cgrp=14 if coh>=1976 & coh<=1977
replace cgrp=15 if coh>=1978 & coh<=1979
replace cgrp=16 if coh>=1980 & coh<=1981
replace cgrp=17 if coh>=1982 & coh<=1983
replace cgrp=18 if coh>=1984 & coh<=1985
replace cgrp=19 if coh>=1986 & coh<=1987
replace cgrp=20 if coh>=1988 & coh<=1989

center totsent,prefix(z_) standardize

drop if cgrp==.

gen coef=.
replace se=.
gen id=_n if _n<21

quietly forval i=1/20{
	reghdfe z_totsent price_nsadefl $gallupX if cgrp==`i' [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
	replace coef=_b[price_nsadefl] if cgrp==`i'
	replace se=_se[price_nsadefl] if cgrp==`i'
}
collapse coef se,by(cgrp)
gen ub=coef+1.96*se
gen lb=coef-1.96*se

merge 1:1 cgrp using stlouis_cpigas_annual_cohort.dta
keep if _merge==3
drop _merge
pwcorr coef cpi_gas_by_all,sig

capture label drop grp1
label define grp1 1 "1950-1" 2 "1952-3" 3 "1954-5" 4 "1956-7" 5 "1958-9" 6 "1960-1" 7 "1962-3" 8 "1964-5" 9 "1966-7" 10 "1968-9" 11 "1970-1" 12 "1972-3" 13 "1974-5" 14 "1976-7" 15 "1978-9" 16 "1980-1" 17 "1982-3" 18 "1984-5" 19 "1986-7" 20 "1988-9"
label values cgrp grp1


reg coef cpi_gas_by_all
local temp1: di %3.2f _b[_cons]
local temp2: di %3.2f _b[cpi_gas_by_all]
twoway (lfitci coef cpi_gas_by_all) (scatter coef cpi_gas_by_all, mlabel(cgrp)), xtitle("Ratio of CPI for Gasoline to Average CPI (1982-4 = 100)") ytitle("Elasticity of Economic Sentiment to Gas Prices") legend(off) note(Elasticity of Sentiment to Gas Price = `temp1' + `temp2'*Gas Price to CPI at Age 16)
graph export FigureA2.pdf, as(pdf) replace

*-------------------------------- Table A.1 Summary Statistics for U.S. Gallup Daily Data


use us_daily_all_clean.dta, clear
merge m:1 zip using zip10_14.dta
keep if _merge==3
drop _merge

center state_of_econ future_of_econ lifeladder_now,prefix(z_)
gen college=cond(educ_coll==1 | educ_postgrad==1,1,0)
tab incgrp,gen(incgrp)
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodlifesat_now=cond(lifeladder_now>=7,1,0)
gen goodlifesat_future=cond(lifeladder_future>=7,1,0)
replace goodlifesat_now=. if lifeladder_now==.
replace goodlifesat_future=. if lifeladder_future==.

gen coh=year-age
gen pre1962=0
replace pre1962=1 if coh<1962 // born in 1962 or earlier --> 18+ in 1980 oil crisis
gen highimpactst=. // jp morgan measure of "higher-impact states" (just do control F)
replace highimpactst=1 if st==19 | st==18 | st==20 | st==26 | st==29 | st==39 | st==46
replace highimpactst=1 if st==1 | st==21 | st==22 | st==28 | st==40 | st==47 | st==48
replace highimpactst=0 if st==9 | st==10 | st==11 | st==12 | st==25 | st==24 | st==37 | st==36 | st==42
replace highimpactst=0 if st==2 | st==6 | st==15 | st==32 | st==41 | st==53

global sumstats college male race_white age nchild married employed pre1962 highcommute highincome incgrp1 incgrp2 incgrp3 incgrp4 incgrp5 incgrp6 incgrp7 goodecon getbetter goodlifesat_now goodlifesat_future liquidconst

quietly estpost sum $sumstats if year>=2008 & year<=2010 [aw=COMB_WEIGHT],d
est sto grp1
quietly estpost sum $sumstats if year>=2011 & year<=2013 [aw=COMB_WEIGHT],d
est sto grp2
quietly estpost sum $sumstats if year>=2014 & year<=2017 [aw=COMB_WEIGHT],d
est sto grp3

la var college "College"
la var male "Male"
la var race_white "White"
la var age "Age"
la var nchild "Children, \#"
la var married "Married"
la var employed	"Employed"
la var pre1962 "Born Before 1962, \%"
la var highimpactst "High-impact State, \%"
la var highcommute "High Commute Zipcode, \%"
la var highincome "High Income Zipcode, \%"
la var goodlifesat_now "Current Life Satisfaction $\ge$ 7"
la var goodlifesat_future "Future Life Satisfaction $\ge$ 7"
la var goodecon "Economy is Good"
la var getbetter "Economy is Improving"
la var liquidconst "Struggles to Pay Bills"
la var incgrp1 "Monthly income, <1500"
la var incgrp2 "Monthly income, 1500-2500"
la var incgrp3 "Monthly income, 2500-3500"
la var incgrp4 "Monthly income, 3500-5500"
la var incgrp5 "Monthly income, 5500-6500"
la var incgrp6 "Monthly income, 6500-8500"
la var incgrp7 "Monthly income, >8500"
local dig1 "2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2" // demographics
local dig2 "2 2 2 2 2" // income + sentiment
esttab grp1 grp2 grp3, cells("mean(fmt(`dig1' `dig2')) sd(fmt(`dig1' `dig2' `dig3'))") label refcat(college "\midrule\textbf{\emph{Demographics}}" incgrp1 "\midrule\textbf{\emph{Income/Sentiment}}", nolabel) mgroups("2008-2010" "2011-2013" "2014-2017", pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) collabels("Mean" "SD")  nonum replace fragment 


*-------------------------------- Table A.2 Regressions with Alternative Dependent Variables


use us_daily_all_clean.dta,clear
drop  healthprob_days rel_* hinsure_covered- hinsure_cov_medicaid
drop if age==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen totsent=state_of_econ+future_of_econ
gen day=day(intdate)
merge m:1 st year month day using OPIS_clean.dta
keep if _merge==3
drop _merge
gen age2=age^2
egen tid=group(intdate)
gen goodlifesat_now=cond(lifeladder_now>=7,1,0)
gen goodlifesat_future=cond(lifeladder_future>=7,1,0)
replace goodlifesat_now=. if lifeladder_now==.
replace goodlifesat_future=. if lifeladder_future==.

center totsent state_of_econ future_of_econ lifeladder_now lifeladder_future,prefix(z_) standardize

global gallupX employed married male age age2 educ_nohs educ_tech educ_somecoll educ_coll educ_postgrad race_white race_black

reghdfe z_lifeladder_now price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
est sto temp11
reghdfe z_lifeladder_future price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
est sto temp12
reghdfe goodlifesat_now price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
est sto temp13
reghdfe goodlifesat_future price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
est sto temp14
reghdfe liquidconst price_nsadefl $gallupX [aw=COMB_WEIGHT],a(st#year#qtr tid) vce(cluster st)
estadd local hasX "Yes",replace
estadd local hasstt "Yes",replace
estadd local hast "Yes",replace
est sto temp15

la var price_nsadefl "State Gas Price"
esttab temp11 temp12 temp13 temp14 temp15, b(3) replace star(* 0.10 ** 0.05 *** 0.01) mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)" "(7)" "(8)") nonum brackets se mgroups("Current Life Satisfaction (z-score)" "Future Life Satisfaction (z-score)" "Good Current Life Satisfaction" "Good Future Life Satisfaction" "Struggles to Pay Bills", pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) label keep(price_nsadefl) stats (r2 N hasX hasstt hast, label("R-squared" "Sample Size" "Controls" "State x Year x Quarter FE" "Day of the Year FE") fmt(2 0)) parentheses nolz nogaps fragment nolines prehead("Dep. var. = ")


*-------------------------------- Table A.3 Correlations of Economic Sentiment and Subjective Well-Being Variables


use us_daily_all_clean.dta,clear
drop  healthprob_days rel_* hinsure_covered- hinsure_cov_medicaid
drop if age==.
gen totsent=state_of_econ+future_of_econ
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodlifesat_now=cond(lifeladder_now>=7,1,0)
gen goodlifesat_future=cond(lifeladder_future>=7,1,0)
replace goodlifesat_now=. if lifeladder_now==.
replace goodlifesat_future=. if lifeladder_future==.


center totsent state_of_econ future_of_econ lifeladder_now lifeladder_future,prefix(z_) standardize

corr z_lifeladder_now z_lifeladder_future goodlifesat_now goodlifesat_future liquidconst z_totsent goodecon getbetter


*-------------------------------- Social explorer zipcode

*https://www.socialexplorer.com/tables/ACS2014_5yr/R11743387
quietly infile using zip10_14dic.dct, using(zip10_14data.txt) clear
renvars STATE COUNTY ZCTA5 \ st county zip
renvars T147_001 T057_001 \ commute medhhinc
keep zip st county commute medhhinc
quietly destring,replace
replace county=1000*st+county
quietly sum commute,d
gen highcommute=cond(commute>`r(p50)',1,0)
replace highcommute=. if commute==.
quietly sum medhhinc,d
gen highincome=cond(medhhinc>`r(p50)',1,0)
replace highincome=. if medhhinc==.
save zip10_14.dta,replace


*2005-09 state: https://www.socialexplorer.com/tables/ACS2009_5yr/R11778993
quietly infile using st2005dic.dct, using(st2005data.txt) clear
ren FIPS st
destring st,replace
ren T001_001 totpop
keep st totpop
save st2005.dta,replace


*-------------------------------- BEA Consumption/capita


*total consumption
import excel using "bea_state_percapita_cons.xls", clear firstrow sheet("clean")
destring,replace
replace st=st/1000
keep if category=="Personal consumption expenditures"
renvars E-X \ y1997 y1998 y1999 y2000 y2001 y2002 y2003 y2004 y2005 y2006 y2007 y2008 y2009 y2010 y2011 y2012 y2013 y2014 y2015 y2016
reshape long y, i(st) j(year)
ren y stconspc_tot
keep stconspc_tot st year
save bea_pce_state_total.dta,replace
*gasoline and other
import excel using "bea_state_percapita_cons.xls", clear firstrow sheet("clean")
destring,replace
replace st=st/1000
keep if category=="      Gasoline and other energy goods"
renvars E-X \ y1997 y1998 y1999 y2000 y2001 y2002 y2003 y2004 y2005 y2006 y2007 y2008 y2009 y2010 y2011 y2012 y2013 y2014 y2015 y2016
reshape long y, i(st) j(year)
ren y stconspc_gas
keep stconspc_gas st year
save bea_pce_state_gas.dta,replace


*put together and deflate
use bea_pce_state_gas.dta,clear
merge 1:1 st year using bea_pce_state_total.dta
keep if _merge==3
drop _merge
merge m:1 year using stlouis_annual_pcedeflator.dta
keep if _merge==3
drop _merge
tsset st year
local todefl "stconspc_gas stconspc_tot"
quietly foreach var in `todefl' {
	replace `var'=`var'*100/pce_defl
	gen dl`var'=(`var'-L.`var')/L.`var'
	gen l`var'=log(`var')
}
drop pce_defl
ren stconspc_gas gaspercapita
collapse gaspercapita,by(st)
save gaspercapita.dta,replace

*bin states into share of energy as total
use bea_pce_state_total.dta, clear
merge 1:1 st year using bea_pce_state_gas.dta
keep if _merge==3
drop _merge
gen eshare=stconspc_gas/stconspc_tot
collapse eshare,by(st)
xtile eshareg=eshare,nq(10)
save bea_pce_eshare_group.dta, replace

*baseline energy share
use bea_pce_state_total.dta, clear
merge 1:1 st year using bea_pce_state_gas.dta
keep if _merge==3
drop _merge
keep if year==2005
gen eshare2005=stconspc_gas/stconspc_tot
collapse eshare2005,by(st)
save bea_pce_eshare2005.dta, replace



*-------------------------------- Estimate state gas price elasticities


use us_daily_all_clean.dta,clear
drop  healthprob_days rel_* hinsure_covered- hinsure_cov_medicaid
drop if age==. | future_of_econ==.
gen getbetter=cond(future_of_econ==3,1,0)
replace getbetter=. if future_of_econ==.
gen goodecon=cond(state_of_econ==3 | state_of_econ==4,1,0)
replace goodecon=. if state_of_econ==.
gen totsent=state_of_econ+future_of_econ
drop day
gen day=day(intdate)
merge m:1 st year month day using OPIS_clean.dta
keep if _merge==3
drop _merge
gen age2=age^2
egen tid=group(intdate)
egen stid=group(st)

center totsent state_of_econ future_of_econ,prefix(z_) standardize

global gallupX employed married male age age2 educ_nohs educ_tech educ_somecoll educ_coll educ_postgrad race_white race_black

gen coef=.
gen se=.

quietly sum stid,d
quietly forval i=1/`r(max)'{
	reghdfe z_totsent price_nsadefl $gallupX  if stid==`i' [aw=COMB_WEIGHT],a(year#qtr) vce(cluster st)
	replace coef=_b[price_nsadefl] if stid==`i'
	replace se=_se[price_nsadefl] if stid==`i'
}
collapse coef se,by(st)

save state_gasprice_elasticities.dta,replace


 
 
